package top.wshape1.easyscan.custom;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import top.wshape1.easyscan.R;
import top.wshape1.easyscan.activity.EasyScanActivity;
import top.wshape1.easyscan.adapter.CustomRuleAdapter;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-05-20
 */
public class CustomRuleListDialog extends Dialog {

    private View customRuleListLayout;


    private EasyScanActivity mainActivity;

    public CustomRuleListDialog(@NonNull Context context) {
        super(context, R.style.BackgroundTransparentStyle);
        mainActivity = (EasyScanActivity) context;
    }

    // 先执行onCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        customRuleListLayout = View.inflate(mainActivity, R.layout.dialog_custom_rule_list, null);

        setContentView(customRuleListLayout);

        setCanceledOnTouchOutside(false);

        customRuleListLayout.findViewById(R.id.iv_custom_rule_back).setOnClickListener(v -> dismiss());
        customRuleListLayout.findViewById(R.id.iv_custom_rule_add).setOnClickListener(v -> {

            CustomRuleAddDialog customRuleAddDialog = new CustomRuleAddDialog(mainActivity);

            customRuleAddDialog.show();

            dismiss();
        });

        ListView listView = customRuleListLayout.findViewById(R.id.lv_custom_rule_list);

        CustomRuleAdapter customRuleAdapter = new CustomRuleAdapter(mainActivity, mainActivity.settingData.getCustomRuleList());
        listView.setAdapter(customRuleAdapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            CustomRuleAddDialog customRuleAddDialog = new CustomRuleAddDialog(mainActivity);
            customRuleAddDialog.setCustomRule(customRuleAdapter.getItem(position));
            customRuleAddDialog.show();
            dismiss();
        });


    }
}
